﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BGSpaceShooter;
using Microsoft.Xna.Framework;

namespace BGSpaceShooterServer
{
    internal class StarFighter : ICollidable
    {
        public StarFighter()
        {
            WorkingEngines = new List<EngineName>();
        }

        public Vector3 Acceleration { get; set; }
        public Vector3 MaxVelocity { get; set; }
        public Vector3 Position { get; set; }
        public int LastPlayerIdHit = -1;

        //public Vector3 Angle { get; set; }
        private Matrix rotation;
        public Matrix Rotation
        {
            get
            {
                return rotation;
            }
            set
            {
                Quaternion rot = Quaternion.CreateFromRotationMatrix(value);
                rot.Normalize();
                rotation = Matrix.CreateFromQuaternion(rot);
            }
        }
        public Vector3 Velocity { get; set; }
        public Vector3 RotationVel { get; set; }
        public bool HPChanged { get; set; }
        private int hp;
        public int HP
        {
            get
            {
                return hp;
            }
            set
            {
                HPChanged = true;
                hp = value;
            }
        }
        public int MaxHP { get; set; }

        public List<EngineName> WorkingEngines { get; set; }


        public float HealthPercent
        {
            get
            {
                if (HP < 0)
                {
                    return 0;
                }
                return (float)HP / (float)MaxHP;
            }
        }

        public BoundingSphere BoundingSphere { get; set; }

        public List<StarFighterComponent> StarFighterComponents { get; set; }

        public StarFighterComponent GetComponentByName(string name)
        {
            return StarFighterComponents.Where(s => s.MeshName == name).First();
        }

        /// <summary>
        /// Komunikat o stanie gracza. Taka strukture przetwarza pozniej klient. Uwaga: Player uzupelnia ten status response o swoje ID! Korzystac z Player.GetStatusResponse()
        /// </summary>
        /// <returns></returns>
        public ServerStatusResponse GetStatusResponse()
        {
            ServerStatusResponse serverStatusResponse = new ServerStatusResponse();
            serverStatusResponse.Position = Position;
            serverStatusResponse.Rotation = Rotation;
            serverStatusResponse.Engines = WorkingEngines;
            serverStatusResponse.Velocity = Velocity;
            return serverStatusResponse;
        }

        /// <summary>
        /// Resetuje zmienne ktore sa obliczane od nowa w kazdej klatce (np. stan silnikow przed obliczeniem klatki)
        /// </summary>
        public void ResetStarFighterState()
        {
            WorkingEngines.Clear();
        }
    }
}
